load("@fbsource//tools/build_defs:rust_library.bzl", "rust_library")
load("@fbsource//tools/build_defs:rust_unittest.bzl", "rust_unittest")

oncall("scm_server_infra")

rust_library(
    name = "mercurial_revlog",
    srcs = glob(["revlog/**/*.rs"]),
    autocargo = {"cargo_toml_dir": "revlog"},
    named_deps = {
        "futures-old": "fbsource//third-party/rust:futures-old",
        "futures_ext": "//common/rust/shed/futures_01_ext:futures_01_ext",
    },
    test_deps = [
        "fbsource//third-party/rust:assert_matches",
        ":mercurial_types-mocks",
        "//common/rust/shed/failure_ext:failure_ext",
        "//eden/mononoke/mononoke_macros:mononoke_macros",
    ],
    # A test inside this target is using #[should_panic], setting the backtrace
    # to false here, otherwise the test binary will try to extract the backtrace
    # and the test will fail with leak sanitizer.
    test_env = {"RUST_BACKTRACE": "0"},
    test_labels = ["tpx-rust-no-backtrace-workaround"],
    test_srcs = glob([
        "revlog/bundle2/fixtures/*",
        "revlog/*/*.bin",
    ]),
    deps = [
        "fbsource//third-party/rust:anyhow",
        "fbsource//third-party/rust:ascii",
        "fbsource//third-party/rust:bitflags",
        "fbsource//third-party/rust:bytes",
        "fbsource//third-party/rust:flate2",
        "fbsource//third-party/rust:memmap2",
        "fbsource//third-party/rust:nom",
        "fbsource//third-party/rust:thiserror",
        ":mercurial_types",
        "//eden/scm/lib/lz4-pyframe:lz4-pyframe",
    ],
)

rust_library(
    name = "mercurial_types",
    srcs = glob(["types/src/**/*.rs"]),
    autocargo = {"cargo_toml_dir": "types"},
    named_deps = {
        "bincode": "fbsource//third-party/rust:bincode-2",
        "blake3-constants": "//blake3:blake3-constants-rust-nothrift",
    },
    test_deps = [
        "fbsource//third-party/rust:tokio",
        "//common/rust/shed/facet:facet",
        "//common/rust/shed/fbinit:fbinit",
        "//common/rust/shed/fbinit:fbinit-tokio",
        "//eden/mononoke/mononoke_macros:mononoke_macros",
        "//eden/mononoke/mononoke_types/serialization:mononoke_types_serialization-rust",
        "//eden/mononoke/repo_attributes/bonsai_hg_mapping:bonsai_hg_mapping",
        "//eden/mononoke/repo_attributes/bookmarks:bookmarks",
        "//eden/mononoke/repo_attributes/commit_graph/commit_graph:commit_graph",
        "//eden/mononoke/repo_attributes/repo_blobstore:repo_blobstore",
        "//eden/mononoke/repo_attributes/repo_derived_data:repo_derived_data",
        "//eden/mononoke/repo_attributes/repo_identity:repo_identity",
        "//eden/mononoke/tests/fixtures:fixtures",
        "//eden/scm/lib/manifest-augmented-tree:manifest-augmented-tree",
    ],
    test_srcs = glob([
        "types/src/blobs/*/*.bin",
    ]),
    deps = [
        "fbsource//third-party/rust:anyhow",
        "fbsource//third-party/rust:ascii",
        "fbsource//third-party/rust:async-stream",
        "fbsource//third-party/rust:async-trait",
        "fbsource//third-party/rust:base64",
        "fbsource//third-party/rust:bitflags",
        "fbsource//third-party/rust:blake3",
        "fbsource//third-party/rust:bytes",
        "fbsource//third-party/rust:futures",
        "fbsource//third-party/rust:itertools",
        "fbsource//third-party/rust:percent-encoding",
        "fbsource//third-party/rust:quickcheck",
        "fbsource//third-party/rust:rand",
        "fbsource//third-party/rust:rand_distr",
        "fbsource//third-party/rust:serde",
        "fbsource//third-party/rust:serde_derive",
        "fbsource//third-party/rust:serde_json",
        "fbsource//third-party/rust:thiserror",
        "fbsource//third-party/rust:tokio",
        "//common/rust/shed/cloned:cloned",
        "//common/rust/shed/futures_ext:futures_ext",
        "//common/rust/shed/quickcheck_arbitrary_derive:quickcheck_arbitrary_derive",
        "//common/rust/shed/sorted_vector_map:sorted_vector_map",
        "//common/rust/shed/sql:sql",
        "//common/rust/shed/stats:stats",
        "//eden/mononoke/blobstore:blobstore",
        "//eden/mononoke/common/futures_watchdog:futures_watchdog",
        "//eden/mononoke/manifest:manifest",
        "//eden/mononoke/mercurial/types/if:mercurial-thrift-rust",
        "//eden/mononoke/mononoke_types:mononoke_types",
        "//eden/mononoke/repo_attributes/filestore:filestore",
        "//eden/mononoke/server/context:context",
        "//eden/scm/lib/edenapi/types:edenapi_types",
        "//eden/scm/lib/types:types",
        "//thrift/lib/rust:fbthrift",
    ],
)

rust_library(
    name = "mercurial_types-mocks",
    srcs = glob(["types/mocks/**/*.rs"]),
    autocargo = {"cargo_toml_dir": "types/mocks"},
    test_deps = ["//eden/mononoke/mononoke_macros:mononoke_macros"],
    deps = [
        "fbsource//third-party/rust:byteorder",
        ":mercurial_types",
        "//eden/mononoke/mononoke_types:mononoke_types",
        "//eden/mononoke/mononoke_types:mononoke_types-mocks",
    ],
)

rust_unittest(
    name = "mercurial_types-tests",
    srcs = glob(["types/tests/**/*.rs"]),
    autocargo = {"cargo_toml_dir": "types/mocks"},
    crate = "mercurial_types_test",
    deps = [
        "fbsource//third-party/rust:anyhow",
        "fbsource//third-party/rust:bytes",
        "fbsource//third-party/rust:quickcheck",
        ":mercurial_types",
        ":mercurial_types-mocks",
        "//eden/mononoke/mononoke_macros:mononoke_macros",
        "//eden/mononoke/mononoke_types:mononoke_types",
        "//eden/mononoke/mononoke_types:mononoke_types-mocks",
    ],
)

rust_library(
    name = "mercurial_bundles",
    srcs = glob(["bundles/src/**/*.rs"]),
    autocargo = {"cargo_toml_dir": "bundles"},
    test_deps = [
        "fbcode//common/rust/shed/quickcheck_arbitrary_derive:quickcheck_arbitrary_derive",
        "fbsource//third-party/rust:assert_matches",
        "fbsource//third-party/rust:partial-io",
        "fbsource//third-party/rust:tokio",
        ":mercurial_types-mocks",
        "//common/rust/shed/failure_ext:failure_ext",
        "//common/rust/shed/fbinit:fbinit",
        "//common/rust/shed/fbinit:fbinit-tokio",
        "//eden/mononoke/mononoke_macros:mononoke_macros",
    ],
    test_srcs = glob(["bundles/src/fixtures/*"]),
    deps = [
        "fbsource//third-party/rust:anyhow",
        "fbsource//third-party/rust:async-compression",
        "fbsource//third-party/rust:async-stream",
        "fbsource//third-party/rust:bufsize",
        "fbsource//third-party/rust:byteorder",
        "fbsource//third-party/rust:bytes",
        "fbsource//third-party/rust:futures",
        "fbsource//third-party/rust:itertools",
        "fbsource//third-party/rust:lazy_static",
        "fbsource//third-party/rust:maplit",
        "fbsource//third-party/rust:percent-encoding",
        "fbsource//third-party/rust:pin-project",
        "fbsource//third-party/rust:quickcheck",
        "fbsource//third-party/rust:thiserror",
        "fbsource//third-party/rust:tokio",
        "fbsource//third-party/rust:tokio-util",
        "fbsource//third-party/rust:tracing",
        ":mercurial_mutation",
        ":mercurial_types",
        "//common/rust/shed/futures_ext:futures_ext",
        "//common/rust/shed/futures_stats:futures_stats",
        "//common/rust/shed/justknobs_stub:justknobs",
        "//eden/mononoke/mononoke_macros:mononoke_macros",
        "//eden/mononoke/mononoke_types:mononoke_types",
        "//eden/mononoke/repo_attributes/phases:phases",
        "//eden/mononoke/server/context:context",
        "//eden/scm/lib/revisionstore:revisionstore_types",
        "//eden/scm/lib/types:types",
        "//eden/scm/lib/vlqencoding:vlqencoding",
    ],
)

rust_library(
    name = "mercurial_mutation",
    srcs = glob([
        "mutation/src/**/*.rs",
        "mutation/schemas/**/*.sql",
    ]),
    autocargo = {"cargo_toml_dir": "mutation"},
    named_deps = {
        "bincode": "fbsource//third-party/rust:bincode-2",
    },
    test_deps = [
        "fbsource//third-party/rust:quickcheck",
        "//common/rust/shed/quickcheck_arbitrary_derive:quickcheck_arbitrary_derive",
    ],
    deps = [
        "fbsource//third-party/rust:anyhow",
        "fbsource//third-party/rust:async-trait",
        "fbsource//third-party/rust:bytes",
        "fbsource//third-party/rust:futures",
        "fbsource//third-party/rust:itertools",
        "fbsource//third-party/rust:serde_json",
        "fbsource//third-party/rust:tracing",
        ":mercurial_types",
        "//common/rust/shed/abomonable_string:abomonable_string",
        "//common/rust/shed/facet:facet",
        "//common/rust/shed/futures_retry:futures_retry",
        "//common/rust/shed/justknobs_stub:justknobs",
        "//common/rust/shed/memcache_stub:memcache",
        "//common/rust/shed/sql:sql",
        "//eden/mononoke/common/rust/caching_ext:caching_ext",
        "//eden/mononoke/common/rust/sql_ext:sql_ext",
        "//eden/mononoke/common/sql_construct:sql_construct",
        "//eden/mononoke/mercurial/mutation/if:hg_mutation_entry_thrift-rust",
        "//eden/mononoke/metaconfig:metaconfig_types",
        "//eden/mononoke/mononoke_types:mononoke_types",
        "//eden/mononoke/server/context:context",
        "//eden/scm/lib/edenapi/types:edenapi_types",
        "//eden/scm/lib/types:types",
        "//thrift/lib/rust:fbthrift",
    ],
)

rust_unittest(
    name = "mercurial_mutation_test",
    srcs = glob(["mutation/test/**/*.rs"]),
    autocargo = {"cargo_toml_dir": "mutation"},
    crate = "mercurial_mutation_test",
    deps = [
        "fbsource//third-party/rust:anyhow",
        "fbsource//third-party/rust:async-trait",
        "fbsource//third-party/rust:maplit",
        ":mercurial_mutation",
        ":mercurial_types",
        ":mercurial_types-mocks",
        "//common/rust/shed/fbinit:fbinit",
        "//common/rust/shed/fbinit:fbinit-tokio",
        "//eden/mononoke/common/sql_construct:sql_construct",
        "//eden/mononoke/mononoke_macros:mononoke_macros",
        "//eden/mononoke/mononoke_types:mononoke_types",
        "//eden/mononoke/mononoke_types:mononoke_types-mocks",
        "//eden/mononoke/server/context:context",
    ],
)
